import { type AllGeoJSON, area, centroid } from '@turf/turf';
import { ShapeTool } from './shape';

export class AreaTool extends ShapeTool {
  addLabel = (id: string, coords: GeoJSON.Position[], areas: number[] = []) => {
    const label = `${areas[0].toFixed(3)} km²`;
    this.createLabels(id, [label], coords);
  };

  handleCreate = (feature: GeoJSON.Feature) => {
    const areas = area(feature as GeoJSON.Feature<GeoJSON.Polygon>) / 1000000; // 平方千米
    const coords = centroid(feature as AllGeoJSON).geometry.coordinates;
    this.addLabel(feature.id as string, [coords], [areas]);

    this.draw?.setDrawingMode('simple_select');
  };

  handleDelete = (feature: GeoJSON.Feature) => this.delId(feature.id as string);
}
